﻿using System;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel;
using AfvandingService;
using System.Data.SqlClient;
using System.Data;
using System.Threading;

namespace AfvandingKlient
{
    public partial class Default : System.Web.UI.Page
    {
        private ICentral central;
        private ChannelFactory<ICentral> proxy;
        private List<Pumpestation> pumpestationsListe;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsCallback)
            {
                proxy = new ChannelFactory<ICentral>(new NetTcpBinding(), "net.tcp://localhost:12345/");
                central = proxy.CreateChannel();
            }

            try
            {
                pumpestationsListe = central.PumpestationsListe();
                gridPumpestationer.DataSource = pumpestationsListe;
                DataBind();
            }

            catch
            {
                Response.Write(
                    "<SCRIPT LANGUAGE=\"JavaScript\">alert(\"Kunne ikke oprette forbindelse til webservicen.\")</SCRIPT>");
            }
        }

        protected void btnOpretPumpestation_Click(object sender, EventArgs e)
        {
            Session["proxy"] = proxy;
            Session["OpdatérEllerOpret"] = "Opret";
            Response.Redirect("PumpeStation.aspx");
        }

        protected void btnOpdatérPumpestation_Click(object sender, EventArgs e)
        {
            try
            {
                Session["proxy"] = proxy;
                Session["OpdatérEllerOpret"] = "Opdatér";
                Session["PSnummer"] = gridPumpestationer.SelectedDataKey.Value;
                Response.Redirect("PumpeStation.aspx");
            }

            catch
            {
                Response.Write("<SCRIPT LANGUAGE=\"JavaScript\">alert(\"Vælg venligst en række før du opdaterer!\")</SCRIPT>");
            }
        }

        protected void btnSletPumpestation_Click(object sender, EventArgs e)
        {
            central.SletPumpestation((int)Session["Pumpestationsnr"]);
        }

        protected void btnOpretPumpe_Click(object sender, EventArgs e)
        {
            Session["proxy"] = proxy;
            Session["PumpestationsNr"] = int.Parse(gridPumpestationer.SelectedDataKey.Value.ToString());
            Session["OpdatérEllerOpret"] = "Opret"; 
            Response.Redirect("Pumpe.aspx");
        }

        protected void btnOpdaterPumpe_Click1(object sender, EventArgs e)
        {
            Session["proxy"] = proxy;
            Session["PumpestationsNr"] = int.Parse(gridPumpestationer.SelectedDataKey.Value.ToString());
            Session["OpdatérEllerOpret"] = "Opdatér";
            Response.Redirect("Pumpe.aspx");
        }

        protected void btnSletPumpe_Click(object sender, EventArgs e)
        {
            central.AnmodSletPumpe((int)Session["Pumpestationsnr"], (int)Session["Pumpenr"]);
        }

        protected void btnStartPumpe_Click(object sender, EventArgs e)
        {
            central.AnmodStart((int)Session["Pumpenr"], (int)Session["Pumpestationsnr"]);
        }

        protected void btnStopPumpe_Click(object sender, EventArgs e)
        {
            central.AnmodStop((int)Session["Pumpenr"], (int)Session["Pumpestationsnr"]);
        }

        protected void btnStatus_Click(object sender, EventArgs e)
        {
            double resultat = central.EnPSVandstand(int.Parse(gridPumpestationer.SelectedDataKey.Value.ToString()));
            lblAktuelvandstand.Text = resultat.ToString();
            lblaktueltekst.Text = "Aktuel vandstand:";
            
            try
            {
                chartVandstand.DataBindTable(central.HentLog((int)Session["Pumpestationsnr"]));
            }

            catch (Exception exception)
            {
                Response.Write("Der findes ingen vandstandslog for denne pumpestation\nFejl: " + exception.Message);
            }

            UpdateGUI();
        }

        protected void gridPumpestationer_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            foreach (var pumpestation in pumpestationsListe)
            {
                if (pumpestation.Pumpestationsnr == int.Parse(gridPumpestationer.SelectedDataKey.Value.ToString()))
                {
                    Session["Source"] = pumpestation.Pumpeliste;
                    gridPumper.DataSource = Session["Source"];
                    gridPumper.DataBind();
                    break;
                }
            }

            Session["Pumpestationsnr"] = int.Parse(gridPumpestationer.SelectedDataKey.Value.ToString());
            lblSelectedPumpStation.Text = gridPumpestationer.SelectedDataKey.Value.ToString();

        }

        protected void gridPumper_SelectedIndexChanged1(object sender, EventArgs e)
        {
            UpdateGUI();
            Session["Pumpenr"] = int.Parse(gridPumper.SelectedDataKey.Value.ToString());
            lblSelectedPump.Text = gridPumper.SelectedDataKey.Value.ToString();
        }

        private void UpdateGUI()
        {
            gridPumper.DataSource = Session["Source"];
            gridPumper.DataBind();
        }


    }
}